<?php

class Admin_TestController extends Zend_Controller_Action
{
    public $ajaxable = array(
        'query' => array('json')
    );

    public $allow = array(
        'admin',
        'guest' => 'index',
        'author',
        'editor' => 'delete',
    );

    public $deny = array(
        'author' => 'delete',
    );

    public $allowUsers = array(
        'foo',
        'monzee' => 'delete',
    );

    public $denyUsers = array(
        'monzee' => array('new', 'edit')
    );

    public function init()
    {
        $this->_helper->accessManager->disable(); // disable predispatch check
//        $this->_helper->accessManager->setAclModel(new Admin_Model_Acl('array'));
    }

    public function indexAction()
    {
        $model = $this->_helper->accessManager->getAclModel()->getAdapter();
        $this->view->info = isset($model->info) ? $model->info : 'There is no information on the acl model adapter.';

        $roles = $model->getAllRoles();
        $this->view->roles = array();
        foreach ($roles as $role) {
            $parents = $model->getParentRoles($role);
            $this->view->roles[] = array($role['id'], implode(', ', $this->_getIds($parents)));
        }

        $users = $model->getAllUsers();
        $opts = array();
        $userRoles = array();
        foreach ($users as $user) {
            $userName = $user['username'];
            $opts[$userName] = $userName;
            $userRoles[$userName] = implode(', ', $this->_getIds($model->getUserRoles($user)));
        }

        $this->view->users = new Zend_Form_Element_Select('users', array(
            'id' => 'userSelect',
            'multiOptions' => $opts,
            'decorators' => array('ViewHelper'),
        ));
        $this->view->userRoles = Zend_Json::encode($userRoles);

        $permissions = $model->getAllPermissions();
        $resIds = array();
        $privs = array();
        foreach ($permissions as $rule)  {
            $resId = $rule['resource_id'];
            $priv = null === $rule['privilege'] ? '*' : $rule['privilege'];
            if (!in_array($resId, $resIds)) {
                $resIds[$resId] = $resId;
            }
            if (!isset($privs[$resId])) {
                $privs[$resId] = array();
            }
            $priv = (array) $priv;
            foreach ($priv as $p) {
                if (!in_array($p, $privs[$resId])) {
                    $privs[$resId][] = $p;
                }
            }
        }

        $this->view->resIds = new Zend_Form_Element_Select('resIds', array(
            'id' => 'resId',
            'multiOptions' => $resIds,
            'decorators' => array('ViewHelper'),
            ));
        $this->view->privs = Zend_Json::encode($privs);
    }

    public function queryAction()
    {
        $this->_helper->ajaxContext->initContext();
        $username = $this->_getParam('user');
        $resId = $this->_getParam('resId');
        $resId = '' === $resId ? null : $resId;
        $priv = $this->_getParam('priv');

        $allowed = $this->_helper->accessManager($username, $resId, $priv);
        $this->view->allowed = $allowed;

        $model = $this->_helper->accessManager->getAclModel();
        $rules = $model->getRulesByResourceId($resId);
        $this->view->rules = $rules instanceof Zend_Db_Table_Rowset
                           ? $rules->toArray() : $rules;
    }
    
    protected function _getIds($roles)
    {
        $ret = array();
        foreach ($roles as $role) {
            $ret[] = $role['id'];
        }
        return $ret;
    }
}